package com.jet.member.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jet.core.utils.IdUtil;
import com.jet.member.domain.entity.MemberTag;
import com.jet.member.domain.entity.MemberTagMap;
import com.jet.member.domain.vo.MemberTagMapVo;
import com.jet.member.mapper.MemberTagMapMapper;
import com.jet.member.service.IMemberTagMapService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jet.member.service.IMemberTagService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-03-02
 */
@Service
public class MemberTagMapServiceImpl extends ServiceImpl<MemberTagMapMapper, MemberTagMap> implements IMemberTagMapService {

    private final IMemberTagService memberTagService;

    public MemberTagMapServiceImpl(IMemberTagService memberTagService) {
        this.memberTagService = memberTagService;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchSave(String memberId, List<String> tagIds) {
        deleteByMemberId(memberId);
        List<MemberTagMap> list = tagIds.stream().map(x -> {
            MemberTagMap entity = new MemberTagMap();
            entity.setId(IdUtil.generateLower());
            entity.setMemberId(memberId);
            entity.setTagId(x);
            return entity;
        }).collect(Collectors.toList());
        super.saveBatch(list);
    }

    @Override
    public void deleteByMemberId(String memberId) {
        Wrapper<MemberTagMap> wrapper = new LambdaQueryWrapper<MemberTagMap>()
                .eq(MemberTagMap::getMemberId, memberId);
        super.remove(wrapper);
    }

    @Override
    public void deleteByTagId(String memberId, String tagId) {
        Wrapper<MemberTagMap> wrapper = new LambdaQueryWrapper<MemberTagMap>()
                .eq(MemberTagMap::getMemberId, memberId)
                .eq(MemberTagMap::getTagId, tagId);
        super.remove(wrapper);
    }

    @Override
    public List<MemberTagMapVo> findAllByMemberId(long merchantNo, String memberId) {
        Wrapper<MemberTagMap> wrapper = new LambdaQueryWrapper<MemberTagMap>()
                .eq(MemberTagMap::getMemberId, memberId);
        List<MemberTagMap> tagMaps = super.list(wrapper);
        if (CollectionUtils.isEmpty(tagMaps)) {
            return Collections.emptyList();
        }
        List<String> tagIds = tagMaps.stream().map(MemberTagMap::getTagId).collect(Collectors.toList());
        List<MemberTag> memberTags = memberTagService.findAllByIds(merchantNo, tagIds);
        return tagMaps.stream().map(x -> {
            MemberTag memberTag = memberTags.stream()
                    .filter(y -> y.getId().equals(x.getTagId()))
                    .findFirst().orElse(null);
            if (memberTag == null) {
                return null;
            }
            MemberTagMapVo vo = new MemberTagMapVo();
            vo.setId(memberTag.getId());
            vo.setName(memberTag.getName());
            vo.setColor(memberTag.getColor());
            return vo;
        }).filter(Objects::nonNull).collect(Collectors.toList());
    }

    @Override
    public List<MemberTagMap> findAllByTagIds(List<String> tagIds) {
        if (CollUtil.isEmpty(tagIds)) {
            return Collections.emptyList();
        }
        Wrapper<MemberTagMap> wrapper = new LambdaQueryWrapper<MemberTagMap>()
                .in(MemberTagMap::getTagId, tagIds);
        return super.list(wrapper);
    }
}
